/*
 * @Author: 'Chenfarry' '2925102113@qq.com'
 * @Date: 2024-07-31 19:35:08
 * @LastEditors: chenhuanhuan 2925102113@qq.com
 * @LastEditTime: 2024-08-27 12:17:27
 * @FilePath: \orderApp\utils\global.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { type App } from 'vue'

import { reqAllKitchen } from '@/service/business/goods'
import type { KitchenType } from '@/service/business/goods/type'
import { reqFoodOven, reqStyleAll, reqTasteAll } from '@/service/business/login'
import type {  KitchType, StyleType, TasteType } from '@/service/business/login/type'

/**
 * 请求所有食堂
 */
const getAllKitchen = () => {
  return new Promise<KitchenType>((resolve, reject) => {
    reqAllKitchen().then((res) => {
      if (res.code == 1) {
        resolve(res.data)
        // return res.data
      }
    })
  })
}

/**
 * 
 * @returns 伙食灶列表
 */
const getFoodOven = () => {
  return new Promise<KitchType>((resolve, reject) => {
    reqFoodOven().then((res) => {
      if (res.retCode == 2000) {
        resolve(res.data)
      }
    })
    
  })
}

/**
 * 
 * @returns 口味列表
 */
 const getTasteAll = () => {
  return new Promise<TasteType>((resolve, reject) => {
    reqTasteAll({ ruleType: 3 }).then((res) => {
      if (res.retCode == 2000) {
        resolve(res.data)
      }
    })
    
  })
}
/**
 * 
 * @returns 菜系偏好列表
 */
const getStyleAll = () => {
  return new Promise<StyleType>((resolve, reject) => {
    reqStyleAll().then((res) => {
      if (res.retCode == 2000) {
        resolve(res.data)
      }
    })
    
  })
}

/**
 * 防抖
 */

const debounce = (fn, delay = 200) => {
  let timer
  return function () {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn()
    }, delay)
  }
}

/**
 * 判断是否有单位符号
 */
const getUnit = (str) => {
  return /(rpx|px|%)/i.test(str)
}

const globalMethods = {
  getAllKitchen,
  debounce,
  getUnit,
  getFoodOven,
  getTasteAll,
  getStyleAll
}

export default {
  install(app: App) {
    app.config.globalProperties.$global = globalMethods
  },
}
